<?php
/**
 * Trida umoznuje agenta importovat pres data publisher API a ulozit do 
 * databaze.
 *
 * @author David Dusek
 */
class AgentImport {
    /**
     * XML retezec do ktereho se uklada odpoved z API
     * @var string 
     */
    private $XMLString = null;    
    
    /**
     * Instance tridy DataRequest pro odesilani pozadavku na API data publisheru
     * @var DataRequest 
     */
    private $dataRequest = null;
    
    /**
     * Pripojeni k databazi
     * @var NConnection
     */
    private $db = null;   
    
    /**
     * Data pro jednu konkretniho agenta
     * @var array
     */
    public $data = array();
    
    /**
     * Vychozi kontruktor    
     * @param NConnection $db 
     */
    public function __construct($db) {
        $this->dataRequest = new DataRequest();
        $this->db = $db;
    }
    
    /**
     * Import jednoho agenta do databaze.
     * @param int $id id agenta
     */
    public function importAgent($id) {
        if ($this->getAgentInformation($id)) {            
            $table = new AgentModel($this->db);
            if ($table->get($id)) {
                $table->where("id", $id)->update($this->data);
            } else {
                $table->insert($this->data);
            }
        }
    }
    
    /**
     * Ziskani informaci o agentovi prostrednictvim api. Vysledny XML je
     * ulozen v atributu XMLString.
     * @param int $id id agenta
     * @return bool true pokud se dotaz podari jinak false 
     */
    private function getAgentInformation($id) {
        $parametrs = array(
            "Login"    => DataRequest::$login,
            "Password" => DataRequest::$password,
            "AgentId" => $id
        );
        
        // odeslani pozadavku do na API
        $this->XMLString = $this->dataRequest->sendRequest($parametrs, 
                DataRequest::$pathAgent);        
        
        // rozparsovani XML seznamu nemovitosti
        if ($this->parseAgentInformation()) {
            return true;
        }
        return false;
    }
    
    /**
     * Rozparsovani informaci o agentovi a vlozeni do atributu data.
     * @return true pokud se parsovani zdari jinak false 
     */
    private function parseAgentInformation() {
        if ($this->XMLString === null) {
            return false;
        }
        
        $document = simplexml_load_string($this->XMLString);
        if ($document) {        
            
            $this->data = array(
                "id"            => (int) $document->Data->Id,
                "firstname"     => (string) $document->Data->Firstname,
                "lastname"      => (string) $document->Data->Lastname,
                "email"         => (string) $document->Data->Email,
                "phone"         => (string) $document->Data->PortableTelephone,
            );
        } else {
            return false;
        }
        return true;
    }
    
    /**
     * Funkce pro testovani pozadavku.
     * @param int $id id agenta
     * @return String 
     */
    public function getXMLData($id) {
        $this->getAgentInformation($id);
        return $this->XMLString;
    }
}
